package great.project.jpa.signle.table;

import great.project.jpa.sign.table.SpringDataJpaSearchSingelTableDemo;
import great.project.jpa.sign.table.domain.entity.SysRole;
import great.project.jpa.sign.table.domain.respository.SysRoleRepository;
import lombok.SneakyThrows;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.Is;
import org.hamcrest.core.StringStartsWith;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.endsWith;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 *
 * @author v_zhangshun01
 * @date 2021/8/20 11:42 上午
 */
@SpringBootTest(classes = SpringDataJpaSearchSingelTableDemo.class)
public class ExampleMethod {

    @Resource
    private SysRoleRepository sysRoleRepository;

    @Test
    void example01() {
        final Example<SysRole> of = Example.of(SysRole.builder().id("1169504891467464705").build());
        final Optional<SysRole> one = sysRoleRepository.findOne(of);
        MatcherAssert.assertThat(one.isPresent(), Is.is(true));
        MatcherAssert.assertThat(one.get().getId(), Is.is("1169504891467464705"));
    }

    @Test
    void example02() {

        /*
            相当于 SQL:
            SELECT * FROM sysRole WHERE description LIKE '%管理' AND LOWER(id) LIKE 'F618b%';
         */

        final ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("description", startsWith())
                .withMatcher("id", endsWith().ignoreCase());

        final Example<SysRole> of =
                Example.of(SysRole
                                .builder()
                                .description("管理")
                                .id("F618b")
                                .build(),
                        exampleMatcher);

        final List<SysRole> all = sysRoleRepository.findAll(of);

        MatcherAssert.assertThat(all.size(), Is.is(1));
        MatcherAssert.assertThat(all.get(0).getId(), Is.is("f6817f48af4fb3af11b9e8bf182f618b"));
    }

    @Test
    void example03() {


        final Example<SysRole> id =
                Example
                        .of(
                                SysRole.builder().id("123").build(),
//                               将 id 不再加入条件中（有参数也不用）
                                ExampleMatcher.matching().withIgnorePaths("id")
                        );

        final List<SysRole> all = sysRoleRepository.findAll(id);

        MatcherAssert.assertThat(all.isEmpty(), Is.is(false));
    }

    @Test
    void example04() {
        final Example<SysRole> of = Example.of(
                SysRole.builder().roleCode("t").build(),
                ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.STARTING)
        );
        final List<SysRole> all = sysRoleRepository.findAll(of);
        for (SysRole sysRole : all) {
            MatcherAssert.assertThat(sysRole.getRoleCode(), StringStartsWith.startsWith("t"));
        }
    }


    @SneakyThrows
    @Test
    void example05() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Example<SysRole> of = Example.of(
                SysRole.builder().roleCode("TEST").createTime(sdf.parse("2018-12-20 10:59:04")).createBy("sdfsdf").build(),
                ExampleMatcher.matching()
                        .withMatcher("roleCode", ExampleMatcher.GenericPropertyMatcher::ignoreCase)
                        .withIgnorePaths("createBy", "roleCode")
        );
        final Optional<SysRole> one = sysRoleRepository.findOne(of);

        MatcherAssert.assertThat(one.isPresent(), Is.is(true));
        MatcherAssert.assertThat(one.get().getRoleCode(), Is.is("test"));

    }


    @Test
    void example06() {
        {

            final Example<SysRole> of = Example.of(
                    SysRole.builder().roleCode("TEST").build(),
                    ExampleMatcher.matching()
                            .withTransformer("roleCode", o -> Optional.of("hr"))
            );
            final Optional<SysRole> one = sysRoleRepository.findOne(of);

            MatcherAssert.assertThat(one.isPresent(), Is.is(true));
            MatcherAssert.assertThat(one.get().getRoleCode(), Is.is("hr"));
        }
    }
}
